public class test {
    //leetcode 11.盛最多水的容器
    class Solution {
        public int maxArea(int[] height) {
            int l = 0;
            int r = height.length - 1;
            int max = 0;
            while(l < r){
                int high = Math.min(height[l],height[r]);
                max = Math.max(high * (r - l),max);
                if(height[l] > height[r]){
                    r--;
                }else if(height[l] <= height[r]){
                    l++;
                }
            }
            return max;
        }
    }
    //leetcode 611.有效三角形的个数
    class Solution {
        public int triangleNumber(int[] nums) {
            int len = nums.length;
            Arrays.sort(nums);
            int number = 0;
            int a = 0;
            int b = len - 2;
            int c = len - 1;
            while(a < c){
                while(nums[a] + nums[b] <= nums[c] && a < b){
                    a++;
                }
                if(nums[a] + nums[b] > nums[c] && a < b){
                    number += b - a;
                    b--;
                }else {
                    a = 0;c--;b = c - 1;
                }
            }
            return number;
        }
    }
    //leetcode 992.按奇偶数排序数组 Ⅱ
    class Solution {
        public int[] sortArrayByParityII(int[] nums) {
            int i = 0;
            int j = 1;
            while(i < nums.length){
                if(nums[i] % 2 == 0){
                    i += 2;
                }else if(nums[j] % 2 != 0){
                    j += 2;
                } else {
                    int tmp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = tmp;
                    i += 2;
                    j += 2;
                }
            }
            return nums;
        }
    }
    //leetcode 两数之和
    class Solution {
        public int[] twoSum(int[] price, int target) {
            int left = 0;
            int right = price.length - 1;
            while(left < right){
                int sum = price[left] + price[right];
                if(sum > target){
                    right--;
                }else if(sum < target){
                    left++;
                }else {
                    return new int[]{price[left],price[right]};
                }
            }
            return new int[]{0};
        }
    }
    //leetcode 15.三数之和
    class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> list = new ArrayList<>();
            Arrays.sort(nums);
            int len = nums.length;
            int i = 0;
            while(i < len - 2){
                if(i > 0 && nums[i - 1] == nums[i]){
                    i++;
                    continue;
                }
                int j = i + 1;
                int k = len - 1;
                while(j < k){
                    //去重操作
                    if(j - 1 != i && nums[j] == nums[j-1]){
                        j++;
                        continue;
                    }else if(k + 1 != len && nums[k] == nums[k+1]){
                        k--;
                        continue;
                    }
                    if(nums[j] + nums[k] > -nums[i]){
                        k--;
                    }else if(nums[j] + nums[k] < -nums[i]){
                        j++;
                    }else {
                        List<Integer> list0 = new ArrayList<>();
                        list0.add(nums[i]);
                        list0.add(nums[j]);
                        list0.add(nums[k]);
                        list.add(list0);
                        k--;
                        j++;
                    }
                }
                i++;
            }
            return list;
        }
    }
}
